﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Services.Dictionary;
using SqlSugarAndEntity.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Middlewares.Attributes;

namespace WebApi.Controllers
{
    [Authorize]
    [ApiExplorerSettings(GroupName = "base_api")]
    [Route("api/[controller]")]
    [ApiController]
    public class DictionaryController : ControllerBase
    {
        private IDictionaryService dictionaryService;
        public DictionaryController(IDictionaryService dictionaryService)
        {
            this.dictionaryService = dictionaryService;
        }
        /// <summary>
        /// 根据父节点ID获取这个节点下的全部节点（不包括子节点的子节点）
        /// </summary>
        /// <param name="ParentId">父节点ID</param>
        /// <returns></returns>
        [HttpGet("GetChildrenNodeByParentID")]
        public IActionResult GetChildrenNodeByParentID(decimal ParentId) => Ok(dictionaryService.GetChildrenNodeByParentID(ParentId));
        /// <summary>
        /// 获取全部的字典节点
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAllDictionaryNode")]
        [IgnoreLog]
        public IActionResult GetAllDictionaryNode() => Ok(dictionaryService.GetAllDictionaryNode());
        /// <summary>
        /// 获取懒加载的子节点信息
        /// </summary>
        /// <param name="ParentId">父节点ID</param>
        /// <param name="page">页码</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetLazyNode(decimal? ParentId, int page = 1, int limit = 1000) => Ok(dictionaryService.GetLazyNode(ParentId, page, limit));
        /// <summary>
        /// 新建一个字典节点
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Create([FromBody] sys_dictionary entity) => Ok(dictionaryService.Insert(entity));
        /// <summary>
        /// 更新字典节点
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update([FromBody] sys_dictionary entity) => Ok(dictionaryService.Update(entity, t => new { t.ID }));
        /// <summary>
        /// 删除字典节点
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Delete(decimal ID) => Ok(dictionaryService.Delete(t => t.ID == ID));
    }
}
